package com.example.bishe.service.impl;

import com.example.bishe.entity.Admin;
import com.example.bishe.mapper.AdminMapper;
import com.example.bishe.service.AdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.bishe.util.Encrypt;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LDH
 * @since 2023/05/04
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {


    /**
    * 功能描述: 管理员登录
    * @Param: [admin]
    * @return: com.example.bishe.entity.Admin
    * @Author: LDH
    * @Date: 2023/05/04
    */
    @Override
    public Admin login(Admin admin) {
        String cipher = Encrypt.jiami(admin.getPassword());
        Admin admin1 = baseMapper.getOne(admin.getAccount(), cipher);
        if (admin1 == null) return null;
        return admin1;
    }

    /**
    * 功能描述: 管理员注册
    * @Param: [admin]
    * @return: com.example.bishe.entity.Admin
    * @Author: LDH
    * @Date: 2023/05/04
    */
    @Override
    public Admin register(Admin admin) {
        String cipher = Encrypt.jiami(admin.getPassword());
        Admin admin1 = baseMapper.getOne(admin.getAccount(), cipher);
        if ( admin1 != null) return admin1;
        admin.setPassword(cipher);
        baseMapper.insert(admin);
        return admin;
    }

    /**
    * 功能描述: 管理员编辑
    * @Param: [admin]
    * @return: com.example.bishe.entity.Admin
    * @Author: LDH
    * @Date: 2023/05/04
    */
    @Override
    public Admin update(Admin admin) {
        baseMapper.updateById(admin);
        return admin;
    }
}
